<template>
  <div class="app-container">
    <el-form :model="queryParams" ref="queryForm" size="small" :inline="true" v-show="showSearch" label-width="68px">
      <el-form-item label="花卉名称" prop="name">
        <el-input
          v-model="queryParams.name"
          placeholder="请输入花卉名称"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="花卉别名" prop="nickName">
        <el-input
          v-model="queryParams.nickName"
          placeholder="请输入花卉别名"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>

      <el-form-item label="花卉种类" prop="categoryName">
        <el-select v-model="queryParams.categoryId" placeholder="花卉种类" clearable>
          <el-option
            v-for="item in this.categoryList"
            :key="item.id"
            :label="item.name"
            :value="item.id">
          </el-option>
        </el-select>
      </el-form-item>

      <el-form-item>
        <el-button type="primary" icon="el-icon-search" size="mini" @click="handleQuery">搜索</el-button>
        <el-button icon="el-icon-refresh" size="mini" @click="resetQuery">重置</el-button>
      </el-form-item>
    </el-form>

    <el-row :gutter="10" class="mb8">
      <el-col :span="1.5">
        <el-button
          type="primary"
          plain
          icon="el-icon-plus"
          size="mini"
          @click="handleAdd"
          v-hasPermi="['system:info:add']"
        >新增
        </el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="success"
          plain
          icon="el-icon-edit"
          size="mini"
          :disabled="single"
          @click="handleUpdate"
          v-hasPermi="['system:info:edit']"
        >修改
        </el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="danger"
          plain
          icon="el-icon-delete"
          size="mini"
          :disabled="multiple"
          @click="handleDelete"
          v-hasPermi="['system:info:remove']"
        >删除
        </el-button>
      </el-col>
      <right-toolbar :showSearch.sync="showSearch" @queryTable="getList"></right-toolbar>
    </el-row>

    <el-table v-loading="loading" :data="infoList" @selection-change="handleSelectionChange">
      <el-table-column type="selection" width="55" align="center"/>
      <!--      <el-table-column label="id" align="center" prop="id" />-->
      <el-table-column label="花卉名称" align="center" prop="name"/>
      <el-table-column label="花卉别名" align="center" prop="nickName"/>
      <el-table-column label="花卉种类" align="center" prop="categoryName"/>
      <el-table-column label="花卉图片" align="center" prop="url" width="100">
        <template slot-scope="scope">
          <image-preview :src="scope.row.url" :width="50" :height="50"/>
        </template>
      </el-table-column>
      <!--      <el-table-column label="花卉种类id" align="center" prop="categoryId" />-->

      <el-table-column label="生长周期" align="center" prop="growthCycle"/>
      <el-table-column label="产地" :show-overflow-tooltip="true" align="center" prop="address"/>
      <el-table-column label="操作" align="center" class-name="small-padding fixed-width">
        <template slot-scope="scope">
          <el-button
            size="mini"
            type="text"
            icon="el-icon-edit"
            @click="handleUpdate(scope.row)"
            v-hasPermi="['system:info:edit']"
          >修改
          </el-button>
          <el-button
            size="mini"
            type="text"
            icon="el-icon-delete"
            @click="handleDelete(scope.row)"
            v-hasPermi="['system:info:remove']"
          >删除
          </el-button>
          <el-button
            size="mini"
            type="text"
            icon="el-icon-plus"
            @click="handleRemark(scope.row)"
            v-hasPermi="['system:remark:add']"
          >评论
          </el-button>
        </template>
      </el-table-column>
    </el-table>

    <pagination
      v-show="total>0"
      :total="total"
      :page.sync="queryParams.pageNum"
      :limit.sync="queryParams.pageSize"
      @pagination="getList"
    />

    <!-- 添加或修改花卉信息管理对话框 -->
    <el-dialog :title="title" :visible.sync="open" width="500px" append-to-body>
      <el-form ref="form" :model="form" :rules="rules" label-width="80px">
        <el-form-item label="花卉名称" prop="name">
          <el-input v-model="form.name" placeholder="请输入花卉名称" maxlength="20" show-word-limit/>
        </el-form-item>
        <el-form-item label="花卉别名" prop="nickName">
          <el-input v-model="form.nickName" placeholder="请输入花卉别名" maxlength="20" show-word-limit/>
        </el-form-item>


        <el-form-item label="花卉种类" prop="categoryId">
          <el-select v-model="form.categoryId" placeholder="花卉种类" clearable>
            <el-option
              v-for="item in this.categoryList"
              :key="item.id"
              :label="item.name"
              :value="item.id">
            </el-option>
          </el-select>
        </el-form-item>

        <el-form-item label="花卉图片" prop="url">
          <image-upload v-model="form.url"/>
        </el-form-item>

        <el-form-item label="生长周期" prop="growthCycle">
          <el-input v-model="form.growthCycle" placeholder="请输入内容" maxlength="50" show-word-limit/>
        </el-form-item>
        <el-form-item label="产地" prop="address">
          <el-input v-model="form.address" placeholder="请输入产地" maxlength="50" show-word-limit/>
        </el-form-item>
<!--        <el-form-item label="备注" prop="remark">-->
<!--          <el-input v-model="form.remark" type="textarea" placeholder="请输入内容" maxlength="200" show-word-limit/>-->
<!--        </el-form-item>-->
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button type="primary" @click="submitForm">确 定</el-button>
        <el-button @click="cancel">取 消</el-button>
      </div>
    </el-dialog>


    <el-dialog title="" :visible.sync="remarkVisible" width="80%" append-to-body>
      <el-row :gutter="10" class="mb8">
        <el-col :span="1.5">
          <el-button
            type="primary"
            plain
            icon="el-icon-plus"
            size="mini"
            @click="handleAddRemark"
            v-hasPermi="['system:remark:add']"
          >发表评论
          </el-button>
        </el-col>
        <right-toolbar :showSearch.sync="showSearchRemark" @queryTable="getRemarkList"></right-toolbar>
      </el-row>

      <el-table v-loading="loadingRemark" :data="remarkList">
        <el-table-column label="编号" type="index" width="55" align="center"/>
        <el-table-column label="用户名称" align="center" prop="userName"/>
        <el-table-column label="评论内容" align="center" prop="content"/>
        <el-table-column label="评论时间" align="center" prop="createTime"/>
      </el-table>

      <pagination
        v-show="remarkTotal>0"
        :total="remarkTotal"
        :page.sync="remarkQueryParams.pageNum"
        :limit.sync="remarkQueryParams.pageSize"
        @pagination="getRemarkList"
      />
    </el-dialog>

    <!-- 添加或修改花卉信息评论对话框 -->
    <el-dialog :title="titleAddRemark" :visible.sync="openRemark" width="500px" append-to-body>
      <el-form ref="formRemark" :model="formRemark" :rules="rulesRemark" label-width="80px">
        <el-form-item label="评论内容" prop="content">
          <el-input v-model="formRemark.content" type="textarea" placeholder="请输入内容" maxlength="500"
                    show-word-limit/>
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button type="primary" @click="submitRemarkForm">确 定</el-button>
        <el-button @click="cancelRemark">取 消</el-button>
      </div>
    </el-dialog>

  </div>
</template>

<script>
import {listInfo, getInfo, delInfo, addInfo, updateInfo} from "@/api/system/info";
import {listAllCategory} from "@/api/system/category";
import {addRemark, listRemark} from "@/api/system/remark";

export default {
  name: "Info",
  data() {
    return {
      // 评论信息
      remarkList: [],

      remarkVisible: false,
      // 遮罩层
      loading: true,

      loadingRemark: true,
      // 选中数组
      ids: [],
      // 非单个禁用
      single: true,
      // 非多个禁用
      multiple: true,
      // 显示搜索条件
      showSearch: true,
      // 显示搜索条件
      showSearchRemark: true,
      // 总条数
      total: 0,
      // 花卉信息表格数据
      remarkTotal: 0,
      // 花卉信息管理表格数据
      infoList: [],
      // 花卉信息分类管理表格数据
      categoryList: [],
      // 弹出层标题
      title: "",
      titleAddRemark: "",
      // 是否显示弹出层
      open: false,
      flowerInfoId: null,
      openRemark: false,
      // 查询参数
      queryParams: {
        pageNum: 1,
        pageSize: 10,
        name: null,
        nickName: null,
        categoryId: null,
        categoryName: null,
        growthCycle: null,
        address: null,
      },
      // 查询参数
      remarkQueryParams: {
        pageNum: 1,
        pageSize: 50,
        name: null,
        flowerInfoId: null,
        nickName: null,
        categoryId: null,
        categoryName: null,
        growthCycle: null,
        address: null,
      },
      // 表单参数
      form: {},

      formRemark: {},

      // 表单校验
      rules: {
        name: [
          {required: true, message: "花卉名称不能为空", trigger: "blur"}
        ],
        categoryId: [
          {required: true, message: "花卉种类不能为空", trigger: "blur"}
        ],
        url: [
          {required: true, message: "花卉图片不能为空", trigger: "blur"}
        ],
      },
      // 表单校验
      rulesRemark: {
        content: [
          {required: true, message: "评论内容不能为空", trigger: "blur"}
        ]
      }
    };
  },
  created() {
    this.getList();
  },
  methods: {
    /** 新增按钮操作 */
    handleAddRemark() {
      this.reset();
      this.openRemark = true;
      this.titleAddRemark = "发表评论";
    },


    /** 查询花卉信息评论列表 */
    getRemarkList() {
      this.loadingRemark = true;
      listRemark(this.remarkQueryParams).then(response => {
        this.remarkList = response.rows;
        this.remarkTotal = response.total;
        this.loadingRemark = false;
      });
    },
    handleRemark(row) {
      this.remarkVisible = true;
      this.remarkQueryParams.flowerInfoId = row.id;
      this.flowerInfoId = row.id
      listRemark(this.remarkQueryParams).then(response => {
        this.remarkList = response.rows;
        this.remarkTotal = response.total;
        this.loadingRemark = false;
      });
    },


    /** 查询花卉信息管理列表 */
    getList() {
      this.loading = true;
      listInfo(this.queryParams).then(response => {
        this.infoList = response.rows;
        this.total = response.total;
        this.loading = false;
        this.getCategoryAllList()
      });
    },

    /** 查询花卉信息分类管理列表 */
    getCategoryAllList() {
      listAllCategory().then(response => {
        this.categoryList = response;
        console.log("分类数据>>>>>>", response)
      });
    },
    // 取消按钮
    cancel() {
      this.open = false;
      this.reset();
    },
    cancelRemark() {
      this.openRemark = false;
      this.resetRemark();
    },
    // 表单重置
    resetRemark() {
      this.formRemark = {
        id: null,
        flowerInfoId: null,
        userId: null,
        userName: null,
        content: null,
        delFlag: null,
        createBy: null,
        createTime: null,
        updateBy: null,
        updateTime: null,
        remark: null
      };
      this.resetForm("formRemark");
    },
    // 表单重置
    reset() {
      this.form = {
        id: null,
        name: null,
        nickName: null,
        url: null,
        categoryId: null,
        categoryName: null,
        growthCycle: null,
        address: null,
        delFlag: null,
        createBy: null,
        createTime: null,
        updateBy: null,
        updateTime: null,
        remark: null
      };
      this.resetForm("form");
    },
    /** 搜索按钮操作 */
    handleQuery() {
      this.queryParams.pageNum = 1;
      this.getList();
    },
    /** 重置按钮操作 */
    resetQuery() {
      this.resetForm("queryForm");
      this.handleQuery();
    },
    // 多选框选中数据
    handleSelectionChange(selection) {
      this.ids = selection.map(item => item.id)
      this.single = selection.length !== 1
      this.multiple = !selection.length
    },
    /** 新增按钮操作 */
    handleAdd() {
      this.reset();
      this.open = true;
      this.title = "添加花卉信息管理";
    },
    /** 修改按钮操作 */
    handleUpdate(row) {
      this.reset();
      const id = row.id || this.ids
      getInfo(id).then(response => {
        this.form = response.data;
        this.open = true;
        this.title = "修改花卉信息管理";
      });
    },
    /** 提交按钮 */
    submitRemarkForm() {
      this.$refs["formRemark"].validate(valid => {
        if (valid) {
          this.formRemark.flowerInfoId = this.flowerInfoId
          addRemark(this.formRemark).then(response => {
            this.$modal.msgSuccess("评论成功");
            this.openRemark = false;
            this.getRemarkList();
          });
        }
      });
    },
    /** 提交按钮 */
    submitForm() {
      this.$refs["form"].validate(valid => {
        if (valid) {
          if (this.form.id != null) {
            updateInfo(this.form).then(response => {
              this.$modal.msgSuccess("修改成功");
              this.open = false;
              this.getList();
            });
          } else {
            addInfo(this.form).then(response => {
              this.$modal.msgSuccess("新增成功");
              this.open = false;
              this.getList();
            });
          }
        }
      });
    },
    /** 删除按钮操作 */
    handleDelete(row) {
      const ids = row.id || this.ids;
      this.$modal.confirm('是否确认删除花卉信息管理编号为"' + ids + '"的数据项？').then(function () {
        return delInfo(ids);
      }).then(() => {
        this.getList();
        this.$modal.msgSuccess("删除成功");
      }).catch(() => {
      });
    },
    /** 导出按钮操作 */
    handleExport() {
      this.download('system/info/export', {
        ...this.queryParams
      }, `info_${new Date().getTime()}.xlsx`)
    }
  }
};
</script>
